/**
 *
 */
package jp.seraph.jspf.pose;

import java.util.Map;

import jp.seraph.jsade.core.Player;
import jp.seraph.jsade.core.World;
import jp.seraph.jsade.math.Angle;
import jp.seraph.jsade.math.AngleVelocity;
import jp.seraph.jsade.model.AngleVelocityCalculator;
import jp.seraph.jsade.model.JointIdentifier;
import jp.seraph.jsade.task.AbstractTask;
import jp.seraph.jsade.task.AgentTask;

/**
 *
 *
 */
public class PoseTask extends AbstractTask implements AgentTask {
    public PoseTask(PoseSet aPoseSet){
        mPoseSet = aPoseSet;
    }

    private PoseSet mPoseSet;

    /**
     *
     * @see jp.seraph.jsade.task.AbstractTask#execute(jp.seraph.jsade.core.World, jp.seraph.jsade.core.Player)
     */
    @Override
    public void execute(World aWorld, Player aPlayer) {
        Player tPlayer = aPlayer;
        PoseSet tPoseSet = mPoseSet;

        boolean tFinished = true;

        for (PoseElement tElement : tPoseSet) {
            Pose tPose = tElement.getPose();
            AngleVelocityCalculator tCalculator = tElement.getCalculator();
            for (Map.Entry<JointIdentifier, Angle> tEntry : tPose) {
                AngleVelocity tVelocity = tPlayer.setJointAngle(tEntry.getKey(), tEntry.getValue(), tCalculator);
                if(tFinished && !tVelocity.equals(AngleVelocity.ZERO))
                    tFinished = false;
            }
        }

        if(tFinished)
            complete();
    }
}
